#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : tujia_spider.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-08-13 16:51 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)
import re
import pymongo
import requests
from report_system.utils import db_util
from report_system.utils import date_util
from report_system.utils.log_util import log
from report_system.spider.spider import Spider
from report_system.utils.development_util import debug


class TujiaAccountSpider(Spider):

    def __init__(self):
        Spider.__init__(self, exception_mail_to='bianzhiwei@iyoujia.com')
        self.name = "tujia_account"
        self.token = "eb97bce0-0091-47a4-a175-a13317b5f4a1"
        print(self.token)
        self.mongo_url = db_util.mongodb_test_url_ if debug else db_util.mongodb_url_
        self.mongo_client_ = None
        self.init_db()

    def con(self, set_name):
        """
        获取 mongodb 连接
        :param set_name:
        :return:
        """

        if self.mongo_client_ is None:
            self.mongo_client_ = pymongo.MongoClient(self.mongo_url)
        return self.mongo_client_[self.name][set_name]

    def init_db(self):
        log.info('初始化数据库 ...')
        log.info(self.mongo_url)
        self.con('store').create_index([('id', pymongo.ASCENDING)], unique=True)
        self.con('account').create_index([('store_id', pymongo.ASCENDING)], unique=True)
        self.con('orders').create_index([('store_id', pymongo.ASCENDING), ('order_id', pymongo.ASCENDING)], unique=True)
        self.con('orders').create_index([('checkInDate', pymongo.ASCENDING)], unique=False)
        self.con('orders').create_index([('order_id', pymongo.ASCENDING)], unique=False)
        self.con('orders').create_index([('checkInDate', pymongo.ASCENDING)], unique=False)
        self.con('orders').create_index([('checkOutDate', pymongo.ASCENDING)], unique=False)
        self.con('orders').create_index([('orderTime', pymongo.ASCENDING)], unique=False)
        self.con('orders').create_index([('orderSource', pymongo.ASCENDING)], unique=False)
        self.con('orders').create_index([('orderStatusLabel', pymongo.ASCENDING)], unique=False)
        self.con('orders').create_index([('store_name', pymongo.ASCENDING)], unique=False)
        self.con('orders').create_index([('unitNumber', pymongo.ASCENDING)], unique=False)
        self.con('orders').create_index([('unitId', pymongo.ASCENDING)], unique=False)

    def all_store(self):
        """
        获取途家总账号内的所有门店
        :return:
        """
        url = 'https://bapi.tujia.com/v1/getpcheader'
        post_json = {"user": {"userId": "10742120", "userToken": self.token,
                              "extraInfo": "", "mUserName": ""},
                     "store": {"storeGuid": "7ff7fe40-e705-4576-bc28-5374cc60c264", "groupGuid": ""},
                     "client": {"locale": "zh-cn", "devType": 4, "uID": "1"}, "parameter": {}, "type": 1,
                     "isReloadCache": None}
        self.req(url=url, method='post', handler=self.all_store_h, json=post_json, user_token=self.token)

    def all_store_h(self, r, j):
        if r and r.status_code == 200 and j and "errcode" in j and j['errcode'] == 0:
            log.info("正在爬取所有门店id等门店信息")
            con_store = self.con('store')
            for merchant in j['content']['merchantList']:
                store_id = merchant['storeId']
                con_store.update_one({'id': int(store_id)}, {'$set': merchant}, upsert=True)
            self.req(handler=self.all_store_done)
        else:
            raise ValueError('门店列表出错')

    def all_store_done(self):
        log.info('爬取门店列表完成！')
        self.req(handler=self.store)

    def store(self, store_id=0):
        log.info('开始循环门店进行爬取，请稍等。。。')
        con_store = self.con('store')
        store = con_store.find_one({'id': {'$gte': store_id}},
                                   {'_id': 0, "storeId": 1, "merchantGlobalID": 1,
                                    "active": 1, "c2CMerchant": 1,
                                    "enableAli170": 1,
                                    "hotelId": 1, "merchantId": 1, "name": 1})

        if store is None:
            self.store_done()
            return
        self.req(handler=self.order_list, store=store)

    def order_list(self, store, page_index=0, page_size=100):
        log.info(store)
        guid = store['merchantGlobalID']
        #     https://guanjia.tujia.com/feapi/bapi//v1tns/searchorderlist?_apitsp=1566888547903
        # url = "https://bapi.tujia.com/v1tns/searchorderlist" + str(date_util.time_stamp())
        url = "https://guanjia.tujia.com/feapi/bapi//v1tns/searchorderlist?_apitsp=" + str(date_util.time_stamp())
        post_json = {
            "user": {
                "userId": "10742120",
                "userToken": self.token,
                "extraInfo": "",
                "mUserName": ""
            },
            "store": {
                "storeGuid": guid,
                "groupGuid": guid
            },
            "client": {
                "locale": "",
                "devType": 4,
                "uID": "1"
            },
            "parameter": {
                "enumSearchOrderType": 0,
                "pageIndex": page_index,
                "pageSize": page_size
            },
            "type": 1,
            "isReloadCache": True
        }
        self.req(url=url, method='post', handler=self.order_list_h, json=post_json, store=store,
                 page_index=page_index, page_size=page_size)

    def order_list_h(self, r, j, store, page_index, page_size):
        log.info("正在爬取订单列表 第{}页 每页 {} 条数据  共计 {} 条".format(page_index, page_size, page_size * page_index))
        if r and r.status_code == 200 and j and "errcode" in j and j['errcode'] == 0:
            content = j['content']
            order_list = content['orderList'] if 'orderList' in content else []
            total_count = content['totalCount'] if 'totalCount' in content else 0
            store_id = store['storeId']
            store_name = store['name']
            con_orders = self.con('orders')
            for order in order_list:
                order['store_name'] = store_name
                order_id = str(order['orderNumber'])
                con_orders.update({'store_id': int(store_id), 'order_id': str(order_id)}, {'$set': order}, upsert=True)
            if (page_index + 1) * page_size < total_count:
                self.req(handler=self.order_list, store=store, page_index=page_index + 1, page_size=page_size)
            else:
                self.order_list_done(store_id=store_id)

    def order_list_done(self, store_id):
        log.info("开始请求下一个门店！！")
        self.req(handler=self.store, store_id=store_id + 1)

    def store_done(self):
        self.matching_order_id()
        log.info("爬虫完成！！ 正在处理对应有家id")

    def matching_order_id(self):
        sql = """select id,third_id 
                 from youjia.orders 
                 where order_channel = 3 
                 and date(create_time) between '{st}' and '{et}'
              """.format(st=date_util.curdate(7), et=date_util.curdate(0))

        con_orders = self.con('orders')
        for idx, row in db_util.records_select_df(sql).iterrows():
            yj_order_id = row['id']
            tj_order_id = row['third_id']
            con_orders.update({'order_id': str(tj_order_id)}, {'$set': {"yj_order_id": yj_order_id}})
            log.info("途家ID:{} 有家ID{} 更新完毕".format(tj_order_id, yj_order_id))

    def run(self):
        # todo 浏览器登录
        self.req(self.all_store)
        Spider.run(self)
        # print(token)


if __name__ == '__main__':
    t = TujiaAccountSpider()
    t.run()
